﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace lxwmvc
{
    /// <summary>
    /// 功能:提供简单的js操作基本方法
    /// [09.4.10 lxw]<para />
    /// </summary>
    public class jshelper 
    {
        /// <summary>
        /// 功能:Response.Write javascript 语句
        /// [09.4.10 lxw]<para />
        /// </summary>
        /// <param name="strscript">javascript语句,里面不能包括&lt;script...&gt; &lt;/script&gt;   </param>
        public static void echo(string strscript) 
        {
            responsescript( strscript );
        }

        /// <summary>
        /// 功能:输出到客户端\r\n
        /// [11-03-08 16:04  周二 lxw]<para />
        /// </summary>
        public static string br
        {
            get {
                return encodejavascriptmessage("\r\n");
            }
        }
        
        /// <summary>
        /// 功能:在当前页面输出一个javascript alert提示
        /// [09.4.10 lxw]<para />
        /// </summary>
        /// <param name="strmsg">提示信息</param>
        public static void alert(string strmsg) 
        {
            //注意两侧使用的单引号字符串
            responsescript(" alert('" + encodejavascriptmessage(strmsg) + "'); ");
        }

        /// <summary>
        /// 功能:在当前页面提示并转向到指定页面
        /// [09.4.10 lxw]<para />
        /// </summary>
        /// <param name="strmsg">提示信息,如果提示信息为空字符串，就不提示，直接转向</param>
        /// <param name="strredirectlink">转向到的页面</param>
        public static void alertandredirect(string strmsg,string strredirectlink)
        {
            if (string.IsNullOrEmpty(strmsg))
            {
                responsescript(" location.href='" + encodejavascriptmessage(strredirectlink) + "'; ");
            }
            else
            {
                responsescript(" alert('" + encodejavascriptmessage(strmsg) + "'); location.href='" + encodejavascriptmessage(strredirectlink) + "'; ");
            }
        }

        /// <summary>
        /// 功能:在当前页面的父页面提示并转向到指定页面
        /// [09.4.10 lxw]<para />
        /// </summary>
        /// <param name="strmsg">提示信息,如果提示信息为空字符串，就不提示，直接转向</param>
        /// <param name="strredirectlink">转向到的页面</param>
        public static void alertandparentredirect(string strmsg, string strredirectlink) 
        {
            if (string.IsNullOrEmpty(strmsg))
            {
                responsescript(" parent.location.href='" + encodejavascriptmessage(strredirectlink) + "'; ");
            }
            else
            {
                responsescript(" alert('" + encodejavascriptmessage(strmsg) + "'); parent.location.href='" + encodejavascriptmessage(strredirectlink) + "'; ");
            }
        }

        /// <summary>
        /// 功能:在当前页面提示并关闭当前页面
        /// [09.4.10 lxw]<para />
        /// </summary>
        /// <param name="strmsg">提示信息,如果提示信息为空字符串，就不提示，直接关闭</param>
        public static void alertandclose(string strmsg) 
        {
            if (string.IsNullOrEmpty(strmsg))
            {
                responsescript(" window.close(); ");
            }
            else
            {
                responsescript(" alert('" + encodejavascriptmessage(strmsg) + "'); window.close(); ");
            }
        }

        /// <summary>
        /// 功能:页面转向，用这种转向方法是不会保留上一个页面请求的相关信息,<para />
        /// 如果需要传递相关参数，还必须通过url param传递信息<para />
        /// 如果用Response.Redirect(url),这种方法也能转向，<para />
        /// 还有Server.Tran??也可以转向，这些方法都能保留信息,这是后台转发的意思<para />
        /// [09.4.10 lxw]<para />
        /// </summary>
        /// <param name="strredirectlink"></param>
        public static void redirect(string strredirectlink) 
        {
            //dataloger.writelog("连接" + strredirectlink);
            responsescript(" location.href='" + encodejavascriptmessage(strredirectlink) + "'; ");
        }

        /// <summary>
        /// 功能:关闭当前窗口
        /// [09.4.10 lxw]<para />
        /// </summary>
        public static void close() 
        {
            responsescript(" window.close(); ");
        }

        /// <summary>
        /// 功能:关闭父窗口
        /// [09.4.10 lxw]<para />
        /// </summary>
        public static void parentclose() 
        {
            responsescript(" parent.window.close(); ");
        }

        /// <summary>
        /// 功能:提示信息并返回,如果想保留上次页面的记录，注意使用{behavior:url(#default#savehistory);}
        /// [09.4.10 lxw]<para />
        /// </summary>
        /// <param name="strmsg">提示信息,如果提示信息为空字符串，就不提示，直接返回</param>
        public static void alertandback(string strmsg) 
        {
            if (string.IsNullOrEmpty(strmsg))
            {
                responsescript(" window.history.back(); ");
            }
            else
            {
                responsescript(" alert('" + encodejavascriptmessage(strmsg) + "'); window.history.back(); ");
            }
        }

        /// <summary>
        /// 功能:直接返回
        /// [09.4.10 lxw]<para />
        /// </summary>
        public static void back()
        {
            responsescript(" window.history.back(); ");
        }

        /// <summary>
        /// 功能:输出script语句
        /// [09.4.10 lxw]<para />
        /// </summary>
        /// <param name="strscript">javascript语句</param>
        static void responsescript(string strscript)
        {
            HttpContext.Current.Response.Write
                (
                string.Format("\r\n <script language='javascript'>\r\n {0} \r\n</script>", strscript)
                );
        }


        /// <summary>
        /// 功能:编码javascript里面字符串代码，保障提示消息不会被浏览器断行和引号产生问题<para />
        /// 字符串的两端用单引号间隔
        /// [09.4.10 lxw]<para />
        /// </summary>
        /// <param name="strmsg">javascript里面的字符串,例如 alert('xxxx') 中的"xxxx"字符串需要进行编码。 </param>
        /// <returns>返回编码过的javascript字符串</returns>
        public static string encodejavascriptmessage(string strmsg)
        {
            Regex re = new Regex("\\\\|\\\"|\\\r|\\\n|\\\'|\\<|\\>|\\&", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
            return re.Replace(strmsg, new MatchEvaluator(encodejscript));
        }

        static string encodejscript(Match m)
        {
            int code = (int)m.Value[0];
            string chars = "0123456789ABCDEF";
            int a1 = code & 0xF;
            int a2 = (code & 0xF0) / 0x10;
            // int   a3=(code&0xF00)/0x100;  
            // int   a4=(code&0xF000)/0x1000;  
            // return   "\\u"+chars[a4]+chars[a3]+chars[a2]+chars[a1];  
            return "\\x" + chars[a2] + chars[a1];
        }
    }
}
